<?php

    class ApartmanBookingFormStep0 extends sfForm
    {
        public function configure()
        {
            $this->setWidgets( array(
                    'date_arrive'   => new sfWidgetFormJQueryDate( array( 'image' => '/images/icons/calendar-select.png' ) ),            
                    'date_depart'   => new sfWidgetFormJQueryDate( array( 'image' => '/images/icons/calendar-select.png' ) ),
                    'adults'       => new sfWidgetFormInput(),
                    'children'      => new sfWidgetFormInput(),
            ));
            
            $this->setValidators( array(
                    'date_arrive'   => new sfValidatorDate(array( 'required' => true, 'min' => time() ) ),
                    'date_depart'   => new sfValidatorDate(array( 'required' => true)),
                    'adults'       => new sfValidatorNumber(array('min' => 1, 'required' => true)),
                    'children'       => new sfValidatorNumber( array( 'required' => false) ),
            ));
            
            $dateDiffValidator = new sfValidatorSchemaCompare('date_arrive', sfValidatorSchemaCompare::LESS_THAN_EQUAL, 'date_depart', array(), array( 'invalid' => 'The date of arrive cannot be greater then the date of depart.' ) );
            $this->mergePostValidator( $dateDiffValidator );
            
            if ( sfContext::getInstance()->getUser()->getAttribute('apartman_id', null, 'booking' ) )
            {
                $callback = new sfValidatorCallback( array( 'callback' => array($this, 'checkApartman') ) );
                $this->mergePostValidator( $callback );
            }
        
            $this->widgetSchema->setNameFormat('booking_step0[%s]');
        
            $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);        
        }
      
 
        public function checkApartman( $validator, $values )
        {
            $end = mktime( 23, 59, 59, $values['date_depart']['month'], $values['date_depart']['day'], $values['date_depart']['year'] );
            $start = mktime( 0, 1, 1, $values['date_arrive']['month'], $values['date_arrive']['day'], $values['date_arrive']['year'] );
                 
            $cs = new Criteria();
            $cs->add( ApartmanBookingPeer::CHECK_IN, $end , Criteria::LESS_THAN );
            $cs->addOr( ApartmanBookingPeer::CHECK_OUT, $start , Criteria::GREATER_THAN );
            $bookings = ApartmanBookingPeer::doSelect( $cs );
         
            $excludes = array();
            foreach( $bookings as $booking )
            {
                $excludes[] = $booking->getApartmanId();
            }
            
            if ( array_search( sfContext::getInstance()->getUser()->getAttribute( 'apartman_id', null, "booking" ), $excludes ) !== FALSE )
            {
                throw new sfValidatorError( $validator, 'apartman_already_booked' );                
            }
        
            $apartman = ApartmanPeer::retrieveByPK( sfContext::getInstance()->getUser()->getAttribute( 'apartman_id', null, "booking" ) );
            
            if ( $apartman->getMaxOccupancy() < $values['adults'] + $values['children'] )
                throw new sfValidatorError( $validator, 'apartman_occupancy_alowed_exhausted' );     
        
            return $values;
        }
      
    }
    
?>
